####################################################################################
# Elections Improve Support for State Trial Court Judges in the United States ######
# Real-World Election Experience Analyses ##########################################
# Andrew R. Stone and Michael P. Olson #############################################
####################################################################################

#########################################################################
# Set working directory, load packages ##################################
#########################################################################
# Set working directory
# Load packages
library(tidyverse); library(stargazer)

####################################################################################
# Load data ########################################################################
####################################################################################
# Loading 2021 data
load("survey2021_experience.RData")
# Removing unnecessary column
survey2021$X <- NULL
# Year variable
survey2021$survey_year <- 2021

# Loading 2023 data
load("survey2023_experience.RData")
# Removing unnecessary column
survey2023$X <- NULL
# Year variable
survey2023$survey_year <- 2023
# No legitimacy data for 2023, so setting column as NAs
survey2023$legitimacy_additive <- NA

# Combining the surveys
surveys <- rbind(survey2023,survey2021)

# Binarizing election treatment so 1 is elections 0 is appointment, saving full version of treatment variable
surveys$election_treatment_full <- surveys$election_treatment
surveys$election_treatment <- ifelse(surveys$election_treatment==3,0,1)

# Renaming column
surveys <- plyr::rename(surveys,replace=c("STATE"="abbreviation"))

# Reading in data on state institutional setups
load("real-life-election-experience.RData")

# Joining the two datasets
surveys <- left_join(surveys,real_court_data)
g <- nrow(surveys)

################################################################################
# In-text discussion of percentage of respondents in state with elected judges #
################################################################################

round(prop.table(table(surveys$elected_any_court)),2)

###################
# Table 2, Line 6 #
###################

cat(as.character(format(round(mean(surveys$elected_any_court,na.rm=T),2),nsmall=2)),sep = '\n', file = "figures_tables/live_trial_court_mean.tex")
cat(as.character(format(round(sd(surveys$elected_any_court,na.rm=T),2),nsmall=2)),sep = '\n', file = "figures_tables/live_trial_court_sd.tex")

#####################################################
# Removing DC respondents, NAs for support question #
#####################################################
surveys <- surveys[!is.na(surveys$judge_support) & surveys$abbreviation!="DC",]
  
####################################################################################
# Table of Familiarity with Judicial Elections by State ############################
####################################################################################
#############
# Table F.1 #
#############
# Making data on state institutional setups character
rcd <- real_court_data %>% mutate_all(as.character)

# Recoding to display partisan/nonpartisan data in table along with election/not
# High court
rcd$partisan_sc_elections <- recode(rcd$partisan_sc_elections,"1"="(P)",
                                                                  "0"="(N)",
                                                                  "NA"="")
# Any court
rcd$partisan_any_court <- recode(rcd$partisan_any_court,"1"="(P)",
                                                             "0"="(N)",
                                                             "NA"="")

# Formatting for latex and stargazer
# Replacing 0 with blank
rcd[rcd=="0"] <- ""
# Replacing 1 with checkmark
rcd[rcd=="1"] <- "checkmark"

# Combining election/not data with partisan/nonpartisan data
rcd$elected_sc <- paste(rcd$elected_sc,rcd$partisan_sc_elections,sep=" ")
rcd$elected_any_court <- paste(rcd$elected_any_court,rcd$partisan_any_court,sep="    ")

# Subsetting to data we want to put in the table
rcd <- rcd[,c("state","elected_any_court","elected_sc")]

# Renaming columns
colnames(rcd) <- c("State","Any Court Elected","High Court Judges Elected")

# Stargazer table
rcd_sg <- stargazer(rcd,summary=F,rownames = F,title="Familiarity with Judicial Elections by State",label="sq_descriptive",
                    table.placement = "!ht")
# Latex formatting for checkmark
rcd_sg <- gsub("checkmark","\\checkmark",fixed=T,rcd_sg)
# Latex formatting
rcd_sg <- gsub("ccc","lcc",fixed=T,rcd_sg)
rcd_sg <- gsub("\\centering","\\centering\\scriptsize",fixed=T,rcd_sg)

# Saving table
cat(rcd_sg, sep = '\n', file = "figures_tables/sq_descriptive.tex")

####################################################################################
# Analysis (Support Outcome) #######################################################
####################################################################################

###########
# Table 3 #
###########
# Full version of combined survey dataset
surveys_save <- surveys

### T-test for overall difference in support by election treatment ###
overall <- t.test(surveys$judge_support[surveys$election_treatment==1],
                  surveys$judge_support[surveys$election_treatment==0])

# Treatment effect and p-value, extracted from t-test object
overall_out <- paste(as.character(format(round(-diff(overall$estimate),3),nsmall=3)))
overall_out_deets <- paste(" (p=",as.character(format(round(overall$p.value,3),nsmall=3)),
                   ", n=",nrow(surveys),")",sep="")

# Saving effect and p-value
cat(overall_out,sep = '\n', file = "figures_tables/overall_out.tex")
cat(overall_out_deets,sep = '\n', file = "figures_tables/overall_out_deets.tex")

### Splitting on "any court elected" ###
# Yes any court elected, treatment effect
trial_yes <- t.test(surveys$judge_support[surveys$election_treatment==1 & surveys$elected_any_court==1],
                    surveys$judge_support[surveys$election_treatment==0 & surveys$elected_any_court==1])
# Treatment effect and p-value
trial_yes_out <- paste(as.character(format(round(-diff(trial_yes$estimate),3),nsmall=3)))
trial_yes_out_deets <- paste(" (p=",as.character(format(round(trial_yes$p.value,3),nsmall=3)),
                      ", n=",nrow(surveys[surveys$elected_any_court==1,]),")",sep="")
# Saving effect and p-value
cat(trial_yes_out,sep = '\n', file = "figures_tables/trial_yes_out.tex")
cat(trial_yes_out_deets,sep = '\n', file = "figures_tables/trial_yes_out_deets.tex")

# No any court elected, treatment effect
trial_no <- t.test(surveys$judge_support[surveys$election_treatment==1 & surveys$elected_any_court==0],
                   surveys$judge_support[surveys$election_treatment==0 & surveys$elected_any_court==0])
# Treatment effect and p-value
trial_no_out <- paste(as.character(format(round(-diff(trial_no$estimate),3),nsmall=3)))
trial_no_out_deets <- paste(" (p=",as.character(format(round(trial_no$p.value,3),nsmall=3)),
                   ", n=",nrow(surveys[surveys$elected_any_court==0,]),")",sep="")
# Saving effect and p-value
cat(trial_no_out,sep = '\n', file = "figures_tables/trial_no_out.tex")
cat(trial_no_out_deets,sep = '\n', file = "figures_tables/trial_no_out_deets.tex")

### Splitting on "high court judges elected" ###
# Yes high court elected, treatment effect
sc_yes <- t.test(surveys$judge_support[surveys$election_treatment==1 & surveys$elected_sc==1],
                 surveys$judge_support[surveys$election_treatment==0 & surveys$elected_sc==1])
# Treatment effect and p-value
sc_yes_out <- paste(as.character(format(round(-diff(sc_yes$estimate),3),nsmall=3)))
sc_yes_out_deets <- paste(" (p=",as.character(format(round(sc_yes$p.value,3),nsmall=3)),
                           ", n=",nrow(surveys[surveys$elected_sc==1,]),")",sep="")
# Saving effect and p-value
cat(sc_yes_out,sep = '\n', file = "figures_tables/sc_yes_out.tex")
cat(sc_yes_out_deets,sep = '\n', file = "figures_tables/sc_yes_out_deets.tex")

# No high court elected, treatment effect
sc_no <- t.test(surveys$judge_support[surveys$election_treatment==1 & surveys$elected_sc==0],
                surveys$judge_support[surveys$election_treatment==0 & surveys$elected_sc==0])
# Treatment effect and p-value
sc_no_out <- paste(as.character(format(round(-diff(sc_no$estimate),3),nsmall=3)))
sc_no_out_deets <- paste(" (p=",as.character(format(round(sc_no$p.value,3),nsmall=3)),
                          ", n=",nrow(surveys[surveys$elected_sc==0,]),")",sep="")
# Saving effect and p-value
cat(sc_no_out,sep = '\n', file = "figures_tables/sc_no_out.tex")
cat(sc_no_out_deets,sep = '\n', file = "figures_tables/sc_no_out_deets.tex")

####################################################################################
# Legitimacy Outcome ###############################################################
####################################################################################
# Clearing the working directory
rm(list=setdiff(ls(), "surveys_save"))

#############
# Table F.2 #
#############
# Setting saved version of the full combined survey dataset as what we'll work with
surveys <- surveys_save
# Subsetting to respondents with legitimacy responses (2021 survey only)
surveys <- surveys[!is.na(surveys$legitimacy_additive),]

### T-test for overall difference in legitimacy by election treatment ###
overall <- t.test(surveys$legitimacy_additive[surveys$election_treatment==1],
                  surveys$legitimacy_additive[surveys$election_treatment==0])
# Treatment effect and p-value
overall_out <- paste(as.character(format(round(-diff(overall$estimate),3),nsmall=3)))
overall_out_deets <- paste(" (p=",as.character(format(round(overall$p.value,3),nsmall=3)),
                         ", n=",nrow(surveys),")",sep="")
# Saving effect and p-value
cat(overall_out,sep = '\n', file = "figures_tables/overall_out_legit.tex")
cat(overall_out_deets,sep = '\n', file = "figures_tables/overall_out_deets_legit.tex")

### Splitting on "any court elected" ###
# Yes any court elected, treatment effect
trial_yes <- t.test(surveys$legitimacy_additive[surveys$election_treatment==1 & surveys$elected_any_court==1],
                    surveys$legitimacy_additive[surveys$election_treatment==0 & surveys$elected_any_court==1])
# Treatment effect and p-value
trial_yes_out <- paste(as.character(format(round(-diff(trial_yes$estimate),3),nsmall=3)))#,
trial_yes_out_deets <- paste(" (p=",as.character(format(round(trial_yes$p.value,3),nsmall=3)),
                           ", n=",nrow(surveys[surveys$elected_any_court==1,]),")",sep="")
# Saving effect and p-value
cat(trial_yes_out,sep = '\n', file = "figures_tables/trial_yes_out_legit.tex")
cat(trial_yes_out_deets,sep = '\n', file = "figures_tables/trial_yes_out_deets_legit.tex")

# No any court elected, treatment effect
trial_no <- t.test(surveys$legitimacy_additive[surveys$election_treatment==1 & surveys$elected_any_court==0],
                   surveys$legitimacy_additive[surveys$election_treatment==0 & surveys$elected_any_court==0])
# Treatment effect and p-value
trial_no_out <- paste(as.character(format(round(-diff(trial_no$estimate),3),nsmall=3)))#,
trial_no_out_deets <- paste(" (p=",as.character(format(round(trial_no$p.value,3),nsmall=3)),
                          ", n=",nrow(surveys[surveys$elected_any_court==0,]),")",sep="")
# Saving effect and p-value
cat(trial_no_out,sep = '\n', file = "figures_tables/trial_no_out_legit.tex")
cat(trial_no_out_deets,sep = '\n', file = "figures_tables/trial_no_out_deets_legit.tex")

### Splitting on "high court judges elected" ###

# Yes high court elected, treatment effect
sc_yes <- t.test(surveys$legitimacy_additive[surveys$election_treatment==1 & surveys$elected_sc==1],
                 surveys$legitimacy_additive[surveys$election_treatment==0 & surveys$elected_sc==1])
# Treatment effect and p-value
sc_yes_out <- paste(as.character(format(round(-diff(sc_yes$estimate),3),nsmall=3)))#,
sc_yes_out_deets <- paste(" (p=",as.character(format(round(sc_yes$p.value,3),nsmall=3)),
                         ", n=",nrow(surveys[surveys$elected_sc==1,]),")",sep="")
# Saving effect and p-value
cat(sc_yes_out,sep = '\n', file = "figures_tables/sc_yes_out_legit.tex")
cat(sc_yes_out_deets,sep = '\n', file = "figures_tables/sc_yes_out_deets_legit.tex")

# No high court elected, treatment effect
sc_no <- t.test(surveys$legitimacy_additive[surveys$election_treatment==1 & surveys$elected_sc==0],
                surveys$legitimacy_additive[surveys$election_treatment==0 & surveys$elected_sc==0])
# Treatment effect and p-value
sc_no_out <- paste(as.character(format(round(-diff(sc_no$estimate),3),nsmall=3)))#,
sc_no_out_deets <- paste(" (p=",as.character(format(round(sc_no$p.value,3),nsmall=3)),
                        ", n=",nrow(surveys[surveys$elected_sc==0,]),")",sep="")
# Saving effect and p-value
cat(sc_no_out,sep = '\n', file = "figures_tables/sc_no_out_legit.tex")
cat(sc_no_out_deets,sep = '\n', file = "figures_tables/sc_no_out_deets_legit.tex")

####################################################################################
# Analysis by Partisan Experience (Support Outcome) ################################
####################################################################################
#############
# Table F.3 #
#############
# Clearing the working directory
rm(list=setdiff(ls(), "surveys_save"))

# Setting saved version of the full combined survey dataset as what we'll work with
surveys <- surveys_save

### Splitting our respondents who have experience with elections into partisan and non-partisan subgroups ###
# Full sample (same as above)
overall <- t.test(surveys$judge_support[surveys$election_treatment==1],
                  surveys$judge_support[surveys$election_treatment==0])
# Treatment effect and p-value, extracted from t-test object
overall_out <- paste(as.character(format(round(-diff(overall$estimate),3),nsmall=3)))
overall_out_deets <- paste(" (p=",as.character(format(round(overall$p.value,3),nsmall=3)),
                           ", n=",nrow(surveys),")",sep="")

# State high court
# No state high court elections (same as above)
sc_no <- t.test(surveys$judge_support[surveys$election_treatment==1 & surveys$elected_sc==0],
                surveys$judge_support[surveys$election_treatment==0 & surveys$elected_sc==0])
# Treatment effect and p-value, extracted from t-test object
sc_no_out <- paste(as.character(format(round(-diff(sc_no$estimate),3),nsmall=3)))#,
sc_no_out_deets <- paste(" (p=",as.character(format(round(sc_no$p.value,3),nsmall=3)),
                         ", n=",nrow(surveys[surveys$elected_sc==0,]),")",sep="")

# Partisan state high court elections
# States w/o state high court elections are coded as NA for partisan_sc_elections
sc_partisan <- t.test(surveys$judge_support[surveys$election_treatment==1 & surveys$partisan_sc_elections==1],
                      surveys$judge_support[surveys$election_treatment==0 & surveys$partisan_sc_elections==1])
# Treatment effect and p-value, extracted from t-test object
sc_partisan_out <- paste(as.character(format(round(-diff(sc_partisan$estimate),3),nsmall=3)))#,
sc_partisan_out_deets <- paste(" (p=",as.character(format(round(sc_partisan$p.value,3),nsmall=3)),
                               ", n=",nrow(surveys[surveys$partisan_sc_elections==1,]),")",sep="")
# Saving effect and p-value
cat(sc_partisan_out,sep = '\n', file = "figures_tables/sc_partisan_out.tex")
cat(sc_partisan_out_deets,sep = '\n', file = "figures_tables/sc_partisan_out_deets.tex")

# Non-partisan state high court elections
# States w/o state high court elections are coded as "NA" for partisan_sc_elections
sc_non_partisan <- t.test(surveys$judge_support[surveys$election_treatment==1 & surveys$partisan_sc_elections==0],
                          surveys$judge_support[surveys$election_treatment==0 & surveys$partisan_sc_elections==0])
# Treatment effect and p-value, extracted from t-test object
sc_non_partisan_out <- paste(as.character(format(round(-diff(sc_non_partisan$estimate),3),nsmall=3)))#,
sc_non_partisan_out_deets <- paste(" (p=",as.character(format(round(sc_non_partisan$p.value,3),nsmall=3)),
                                   ", n=",nrow(surveys[surveys$partisan_sc_elections==0,]),")",sep="")
# Saving effect and p-value
cat(sc_non_partisan_out,sep = '\n', file = "figures_tables/sc_non_partisan_out.tex")
cat(sc_non_partisan_out_deets,sep = '\n', file = "figures_tables/sc_non_partisan_out_deets.tex")

# Any court
# Not elected (same as above)
trial_no <- t.test(surveys$judge_support[surveys$election_treatment==1 & surveys$elected_any_court==0],
                   surveys$judge_support[surveys$election_treatment==0 & surveys$elected_any_court==0])
# Treatment effect and p-value, extracted from t-test object
trial_no_out <- paste(as.character(format(round(-diff(trial_no$estimate),3),nsmall=3)))#,
trial_no_out_deets <- paste(" (p=",as.character(format(round(trial_no$p.value,3),nsmall=3)),
                            ", n=",nrow(surveys[surveys$elected_any_court==0,]),")",sep="")

# Any partisan elections
any_partisan <- t.test(surveys$judge_support[surveys$election_treatment==1 & surveys$partisan_any_court==1],
                       surveys$judge_support[surveys$election_treatment==0 & surveys$partisan_any_court==1])
# Treatment effect and p-value, extracted from t-test object
any_partisan_out <- paste(as.character(format(round(-diff(any_partisan$estimate),3),nsmall=3)))#,
any_partisan_out_deets <- paste(" (p=",as.character(format(round(any_partisan$p.value,3),nsmall=3)),
                                ", n=",nrow(surveys[surveys$partisan_any_court==1,]),")",sep="")
# Saving effect and p-value
cat(any_partisan_out,sep = '\n', file = "figures_tables/any_partisan_out.tex")
cat(any_partisan_out_deets,sep = '\n', file = "figures_tables/any_partisan_out_deets.tex")

# Elections but no partisan elections
# States w/o elections are coded as "NA" for partisan_any_court
elections_no_partisan <- t.test(surveys$judge_support[surveys$election_treatment==1 & surveys$partisan_any_court==0],
                                surveys$judge_support[surveys$election_treatment==0 & surveys$partisan_any_court==0])
# Treatment effect and p-value, extracted from t-test object
elections_no_partisan_out <- paste(as.character(format(round(-diff(elections_no_partisan$estimate),3),nsmall=3)))#,
elections_no_partisan_out_deets <- paste(" (p=",as.character(format(round(elections_no_partisan$p.value,3),nsmall=3)),
                                         ", n=",nrow(surveys[surveys$partisan_any_court==0,]),")",sep="")
# Saving effect and p-value
cat(elections_no_partisan_out,sep = '\n', file = "figures_tables/elections_no_partisan_out.tex")
cat(elections_no_partisan_out_deets,sep = '\n', file = "figures_tables/elections_no_partisan_out_deets.tex")

#############
# Table F.4 #
#############
# Clearing the working directory
rm(list=setdiff(ls(), "surveys_save"))

# Setting saved version of the full combined survey dataset as what we'll work with
surveys <- surveys_save

### Splitting respondents by if they have elections and got a matching system in their treatment, those who have elections and didn't get a match, and those with no elections ###

# Full sample (same as above)
overall <- t.test(surveys$judge_support[surveys$election_treatment==1],
                  surveys$judge_support[surveys$election_treatment==0])
# Treatment effect and p-value, extracted from t-test object
overall_out <- paste(as.character(format(round(-diff(overall$estimate),3),nsmall=3)))
overall_out_deets <- paste(" (p=",as.character(format(round(overall$p.value,3),nsmall=3)),
                           ", n=",nrow(surveys),")",sep="")

# State high court #########################################
# People with partisan real-world elections, comparing partisan treated (1) and control (3)
sc_match_partisan <- t.test(surveys$judge_support[(surveys$election_treatment_full==1 & surveys$partisan_sc_elections==1)],
                            surveys$judge_support[(surveys$election_treatment_full==3 & surveys$partisan_sc_elections==1)])
# Treatment effect and p-value, extracted from t-test object
sc_match_partisan_out <- paste(as.character(format(round(-diff(sc_match_partisan$estimate),3),nsmall=3)))
sc_match_partisan_out_deets <- paste(" (p=",as.character(format(round(sc_match_partisan$p.value,3),nsmall=3)),
                                     ", n=",nrow(surveys[(surveys$election_treatment_full%in%c(1,3) & surveys$partisan_sc_elections==1),]),")",sep="")
# Saving effect and p-value
cat(sc_match_partisan_out,sep = '\n', file = "figures_tables/sc_match_partisan_out.tex")
cat(sc_match_partisan_out_deets,sep = '\n', file = "figures_tables/sc_match_partisan_out_deets.tex")

# People with nonpartisan real-world elections, comparing non-partisan treated (2) and control (3)
sc_match_nonpartisan <- t.test(surveys$judge_support[(surveys$election_treatment_full==2 & surveys$partisan_sc_elections==0)],
                               surveys$judge_support[(surveys$election_treatment_full==3 & surveys$partisan_sc_elections==0)])
# Treatment effect and p-value, extracted from t-test object
sc_match_nonpartisan_out <- paste(as.character(format(round(-diff(sc_match_nonpartisan$estimate),3),nsmall=3)))
sc_match_nonpartisan_out_deets <- paste(" (p=",as.character(format(round(sc_match_nonpartisan$p.value,3),nsmall=3)),
                                        ", n=",nrow(surveys[(surveys$election_treatment_full%in%c(2,3) & surveys$partisan_sc_elections==0),]),")",sep="")
# Saving effect and p-value
cat(sc_match_nonpartisan_out,sep = '\n', file = "figures_tables/sc_match_nonpartisan_out.tex")
cat(sc_match_nonpartisan_out_deets,sep = '\n', file = "figures_tables/sc_match_nonpartisan_out_deets.tex")

# People with partisan real-world elections, comparing non-partisan treated (2) and control (3)
sc_mismatch_partisan <- t.test(surveys$judge_support[(surveys$election_treatment_full==2 & surveys$partisan_sc_elections==1)],
                               surveys$judge_support[(surveys$election_treatment_full==3 & surveys$partisan_sc_elections==1)])
# Treatment effect and p-value, extracted from t-test object
sc_mismatch_partisan_out <- paste(as.character(format(round(-diff(sc_mismatch_partisan$estimate),3),nsmall=3)))#,
sc_mismatch_partisan_out_deets <- paste(" (p=",as.character(format(round(sc_mismatch_partisan$p.value,3),nsmall=3)),
                                        ", n=",nrow(surveys[(surveys$election_treatment_full%in%c(2,3) & surveys$partisan_sc_elections==1),]),")",sep="")
# Saving effect and p-value
cat(sc_mismatch_partisan_out,sep = '\n', file = "figures_tables/sc_mismatch_partisan_out.tex")
cat(sc_mismatch_partisan_out_deets,sep = '\n', file = "figures_tables/sc_mismatch_partisan_out_deets.tex")

# People with nonpartisan real-world elections, comparing partisan treated (1) and control (3)
sc_mismatch_nonpartisan <- t.test(surveys$judge_support[(surveys$election_treatment_full==1 & surveys$partisan_sc_elections==0)],
                                  surveys$judge_support[(surveys$election_treatment_full==3 & surveys$partisan_sc_elections==0)])
# Treatment effect and p-value, extracted from t-test object
sc_mismatch_nonpartisan_out <- paste(as.character(format(round(-diff(sc_mismatch_nonpartisan$estimate),3),nsmall=3)))#,
sc_mismatch_nonpartisan_out_deets <- paste(" (p=",as.character(format(round(sc_mismatch_nonpartisan$p.value,3),nsmall=3)),
                                           ", n=",nrow(surveys[(surveys$election_treatment_full%in%c(1,3) & surveys$partisan_sc_elections==0),]),")",sep="")
# Saving effect and p-value
cat(sc_mismatch_nonpartisan_out,sep = '\n', file = "figures_tables/sc_mismatch_nonpartisan_out.tex")
cat(sc_mismatch_nonpartisan_out_deets,sep = '\n', file = "figures_tables/sc_mismatch_nonpartisan_out_deets.tex")

# Any court #####################################
# People with partisan real-world elections, comparing partisan treated (1) and control (3)
any_match_partisan <- t.test(surveys$judge_support[(surveys$election_treatment_full==1 & surveys$partisan_any_court==1)],
                             surveys$judge_support[(surveys$election_treatment_full==3 & surveys$partisan_any_court==1)])
# Treatment effect and p-value, extracted from t-test object
any_match_partisan_out <- paste(as.character(format(round(-diff(any_match_partisan$estimate),3),nsmall=3)))#,
any_match_partisan_out_deets <- paste(" (p=",as.character(format(round(any_match_partisan$p.value,3),nsmall=3)),
                                      ", n=",nrow(surveys[(surveys$election_treatment_full%in%c(1,3) & surveys$partisan_any_court==1),]),")",sep="")
# Saving effect and p-value
cat(any_match_partisan_out,sep = '\n', file = "figures_tables/any_match_partisan_out.tex")
cat(any_match_partisan_out_deets,sep = '\n', file = "figures_tables/any_match_partisan_out_deets.tex")

# People with nonpartisan real-world elections, comparing nonpartisan treated (2) and control (3)
any_match_nonpartisan <- t.test(surveys$judge_support[(surveys$election_treatment_full==2 & surveys$partisan_any_court==0)],
                                surveys$judge_support[(surveys$election_treatment_full==3 & surveys$partisan_any_court==0)])
# Treatment effect and p-value, extracted from t-test object
any_match_nonpartisan_out <- paste(as.character(format(round(-diff(any_match_nonpartisan$estimate),3),nsmall=3)))#,
any_match_nonpartisan_out_deets <- paste(" (p=",as.character(format(round(any_match_nonpartisan$p.value,3),nsmall=3)),
                                         ", n=",nrow(surveys[(surveys$election_treatment_full%in%c(2,3) & surveys$partisan_any_court==0),]),")",sep="")
# Saving effect and p-value
cat(any_match_nonpartisan_out,sep = '\n', file = "figures_tables/any_match_nonpartisan_out.tex")
cat(any_match_nonpartisan_out_deets,sep = '\n', file = "figures_tables/any_match_nonpartisan_out_deets.tex")

# People with partisan real-world elections, comparing nonpartisan treated (2) and control (3)
any_mismatch_partisan <- t.test(surveys$judge_support[(surveys$election_treatment_full==2 & surveys$partisan_any_court==1)],
                                surveys$judge_support[(surveys$election_treatment_full==3 & surveys$partisan_any_court==1)])
# Treatment effect and p-value, extracted from t-test object
any_mismatch_partisan_out <- paste(as.character(format(round(-diff(any_mismatch_partisan$estimate),3),nsmall=3)))#,
any_mismatch_partisan_out_deets <- paste(" (p=",as.character(format(round(any_mismatch_partisan$p.value,3),nsmall=3)),
                                         ", n=",nrow(surveys[(surveys$election_treatment_full%in%c(2,3) & surveys$partisan_any_court==1),]),")",sep="")
# Saving effect and p-value
cat(any_mismatch_partisan_out,sep = '\n', file = "figures_tables/any_mismatch_partisan_out.tex")
cat(any_mismatch_partisan_out_deets,sep = '\n', file = "figures_tables/any_mismatch_partisan_out_deets.tex")

# People with nonpartisan real-world elections, comparing partisan treated (1) and control (3)
any_mismatch_nonpartisan <- t.test(surveys$judge_support[(surveys$election_treatment_full==1 & surveys$partisan_any_court==0)],
                                   surveys$judge_support[(surveys$election_treatment_full==3 & surveys$partisan_any_court==0)])
# Treatment effect and p-value, extracted from t-test object
any_mismatch_nonpartisan_out <- paste(as.character(format(round(-diff(any_mismatch_nonpartisan$estimate),3),nsmall=3)))#,
any_mismatch_nonpartisan_out_deets <- paste(" (p=",as.character(format(round(any_mismatch_nonpartisan$p.value,3),nsmall=3)),
                                            ", n=",nrow(surveys[(surveys$election_treatment_full%in%c(1,3) & surveys$partisan_any_court==0),]),")",sep="")
# Saving effect and p-value
cat(any_mismatch_nonpartisan_out,sep = '\n', file = "figures_tables/any_mismatch_nonpartisan_out.tex")
cat(any_mismatch_nonpartisan_out_deets,sep = '\n', file = "figures_tables/any_mismatch_nonpartisan_out_deets.tex")
